//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include "cfield.h"
#include <buola/algorithm/trim.h>

namespace buola { namespace scene { namespace vrml {

mat::CVec3d CField::ExtractVector()
{
    if(mTokens.size()!=3) throw XInvalid("wrong number of tokens in ExtractVector");
    
    return {to_<double>(mTokens[0].Text()),to_<double>(mTokens[1].Text()),to_<double>(mTokens[2].Text())};
}

mat::CQuat_d CField::ExtractRotation()
{
    if(mTokens.size()!=4) throw XInvalid("wrong number of tokens in ExtractRotation");

    return mat::CQuat_d(to_<double>(mTokens[3].Text()),mat::CVec3d(to_<double>(mTokens[0].Text()),to_<double>(mTokens[1].Text()),
                                                                   to_<double>(mTokens[2].Text())));
}
    
mat::CVec3d CField::ExtractAxis()
{
    if(mTokens.size()<3) throw XInvalid("wrong number of tokens in ExtractRotation");

    return {to_<double>(mTokens[0].Text()),to_<double>(mTokens[1].Text()),to_<double>(mTokens[2].Text())};
}
    
double CField::ExtractOffset()
{
    if(mTokens.size()!=4) throw XInvalid("wrong number of tokens in ExtractRotation");

    return to_<double>(mTokens[3].Text());
}
    
double CField::ExtractNumber()
{
    if(mTokens.size()!=1) throw XInvalid("wrong number of tokens in ExtractVector");
    
    return to_<double>(mTokens[0].Text());
}

img::CColor CField::ExtractColor()
{
    if(mTokens.size()!=3) throw XInvalid("wrong number of tokens in ExtractVector");
    
    return {to_<double>(mTokens[0].Text()),to_<double>(mTokens[1].Text()),to_<double>(mTokens[2].Text())};
}

std::string CField::ExtractString()
{
    if(mTokens.size()!=1) throw XInvalid("wrong number of tokens in ExtractVector");
    
    std::string lText=mTokens[0].Text();
    trim(lText,fn::equal_to('\"'));
    
    return lText;
}


/*namespace vrml*/ } /*namespace scene*/ } /*namespace buola*/ }
